/* Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef __CERTIFICATE_PARSER_H__
#define __CERTIFICATE_PARSER_H__

#include "common.h"

#define CERT_ITEM_LEN_2   (2 + 1)
#define CERT_ITEM_LEN_64  (64 + 1)
#define CERT_ITEM_LEN_128 (128 + 1)
#define CERT_ITEM_LEN_256 (256 + 1)
#define CERT_INFO_LEN     (512 + 1)

enum CertificateType {
    PEM      = 0,
    PEMchain = 1,
    PKCS     = 2
};

enum CertificateUsageType {
    CACert               = 0,
    SSLCert              = 1,
    AccountCert          = 2,
    CMPCert              = 3,
    FirmwareCert         = 4,
    TrustedComponentCert = 5
};

struct CertInfo {
    char country[CERT_ITEM_LEN_2];
    char state[CERT_ITEM_LEN_128];
    char location[CERT_ITEM_LEN_128];
    char organization[CERT_ITEM_LEN_64];
    char organizationUnit[CERT_ITEM_LEN_64];
    char commomName[CERT_ITEM_LEN_64];
    char email[CERT_ITEM_LEN_128];
};

struct CertificateInfo {
    string   subject;
    string   issuer;
    string   serialNumber;
    int64_t  validNotBefore;
    int64_t  validNotAfter;
    uint32_t keyUsage;
    string   keyUsageString;
    string   signatureAlgorithm;
    uint32_t keyLength;
    string   hashVal;
    string   fingerprintAlgorithm;
    string   fingerprint;
    string   publicKey;

    CertificateInfo() : subject(""), issuer(""), serialNumber(""), validNotBefore(0), validNotAfter(0), keyUsage(0),
        keyUsageString(""), signatureAlgorithm(""), keyLength(0), hashVal(""), fingerprintAlgorithm(""),
        fingerprint(""), publicKey("") {}
};

struct NidMap {
    int32_t  nidKey;
    char     *pcert_point;
    size_t   pcert_point_size;
};

struct KeyUsageMap {
    uint32_t flag;
    string   context;
};

tuple<int32_t, CertificateInfo> get_cert_info(const uint8_t *der_data, size_t der_size);

#endif // __CERTIFICATE_PARSER_H__